﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SdmxMl.Structure;


namespace SdmxMl.UC
{
    public partial class UcObjectIdList : UserControl
    {
        private bool _isFinal;
        public bool IsFinal
        {
            set { _isFinal = value; }
        }

        public override string Text
        {
            get { return groupBoxTitle.Text; }
            set { groupBoxTitle.Text = value; }
        }

        private ObjectIDTypeList _objIdList;
        public ObjectIDTypeList ObjIdList
        {
            set 
            { 
                _objIdList = value;
                UpdateObjIdList();
                SetButtonsState();

            }
        }

        public UcObjectIdList()
        {
            InitializeComponent();

            foreach (ObjectIDType o in Enum.GetValues(typeof(ObjectIDType)))
                comboBoxObj.Items.Add(o);
  //          comboBoxObj.SelectedIndex = 0;
            this.comboBoxObj.SelectedIndexChanged += new System.EventHandler(this.comboBoxObj_SelectedIndexChanged);
        }

        private void UpdateObjIdList()
        {
            listBoxObj.Items.Clear();
            if (_objIdList != null)
            {
                foreach (ObjectIDType so in _objIdList)
                    listBoxObj.Items.Add(so);
            }
            if (listBoxObj.Items.Count > 0)
                listBoxObj.SelectedIndex = 0;

        }

        private void SetButtonsState()
        {
            butRemove.Enabled = _isFinal == false && listBoxObj.SelectedIndex >= 0;
        }

        private void comboBoxObj_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxObj.SelectedIndex >= 0)
            {
                _objIdList.Add((ObjectIDType)comboBoxObj.SelectedItem);
                listBoxObj.Items.Add(_objIdList[_objIdList.Count - 1]);
                listBoxObj.SelectedIndex = listBoxObj.Items.Count - 1;
                SetButtonsState();
                comboBoxObj.SelectedIndex = -1;
            }

        }

        private void butAdd_Click(object sender, EventArgs e)
        {

            _objIdList.Add((ObjectIDType)comboBoxObj.SelectedItem);
            listBoxObj.Items.Add(_objIdList[_objIdList.Count - 1]);
            listBoxObj.SelectedIndex = listBoxObj.Items.Count - 1;
            SetButtonsState();

        }

        private void butRemove_Click(object sender, EventArgs e)
        {
            if (listBoxObj.SelectedIndex >= 0)
            {
                _objIdList.RemoveAt(listBoxObj.SelectedIndex);
                listBoxObj.Items.RemoveAt(listBoxObj.SelectedIndex);
                if (listBoxObj.Items.Count > 0)
                    listBoxObj.SelectedIndex = listBoxObj.Items.Count - 1;

                SetButtonsState();
            }
        }
    }
}
